//
//  GYOrderDetailsViewController.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/22.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYOrderDetailsViewController.h"
#import "GYOrderStateCell.h"
#import "GYOrderReceiveInfoCell.h"
#import "GYOrderInfoTableViewCell.h"
#import "GYNetWorkTools.h"
#import "GYOrderDetails.h"
#import "GYReceiveTableViewCell.h"
#import "GYOrdetTimeCell.h"
#import "GYOrderRefundViewController.h"
#import "GYWebViewViewController.h"

@interface GYOrderDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 数据源 */
@property (nonatomic, strong) NSArray *titles;

/** 图片 */
@property (nonatomic, strong) NSArray *images;

/** 订单详情数据模型 */
@property (nonatomic, strong) GYOrderDetails *orderDetailsModel;
@property (weak, nonatomic) IBOutlet UIButton *orderRefundBtu;

@end

@implementation GYOrderDetailsViewController

- (IBAction)orderRefundBtuClick:(UIButton *)sender {
    //订单退款 (判断可不可订单退款)
    if (self.orderDetailsModel.if_refund_cancel == 1) {
        
        //2.判断是否处于退款状态
        if (self.orderDetailsModel.if_lock) {
            UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"该订单退款正在审核中,不能重复操作!" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }else {
            GYOrderRefundViewController *orderRefundVC = [[GYOrderRefundViewController alloc] initWithNibName:@"GYOrderRefundViewController" bundle:nil];
            orderRefundVC.orderGoods = [self.orderDetailsModel.goods_list firstObject];
            orderRefundVC.if_refund_cancel = 1;//代表是否可以全款  并不是说是全额退款
            [self.navigationController pushViewController:orderRefundVC animated:YES];            
        }
    }else {
        [EasyTextView showText:@"该订单正在退款中或则不支持订单退款!"];
    }
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    [self setupUI];
}

- (void)setupUI {
    
    //注册通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refundBtuClick:) name:@"orderDetailsInfoGoods" object:nil];
    
    self.navigationItem.title = @"订单详情";
    //self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(moreBtuClick) button:nil];
    //设置订单按钮的退款通知
    self.orderRefundBtu.layer.cornerRadius = 4;
    self.orderRefundBtu.layer.borderColor = RGBColor(221, 221, 221).CGColor;
    self.orderRefundBtu.layer.borderWidth = 1;
    
    //设置表格的属性
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CGFLOAT_MIN)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CGFLOAT_MIN)];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    

    //加载订单详情数据
    [self loadOrderDetails];
}

/**
 订单退款通知

 @param noti <#noti description#>
 */
- (void)refundBtuClick:(NSNotification *)noti {
    GYOrderRefundViewController *orderRefundVC = [[GYOrderRefundViewController alloc] initWithNibName:@"GYOrderRefundViewController" bundle:nil];
    orderRefundVC.orderGoods = noti.object;
    orderRefundVC.if_refund_cancel = self.orderDetailsModel.goods_list.count > 1 ? 0 : 1;
    [self.navigationController pushViewController:orderRefundVC animated:YES];
}


//- (void)moreBtuClick {
//    NSLog(@"订单详情更多界面....");
//}

- (void)loadOrderDetails {
    //NSLog(@"dsads===%@",self.order_id);
    [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
        EasyLodingConfig *config = [EasyLodingConfig shared];
        config.showOnWindow = YES;
        
        return config;
    }];
    [GYNetWorkTools requestOrderDetails:self.order_id success:^(NSDictionary *obj) {
        //NSLog(@"obj=========%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            NSDictionary *order_info = [datas objectForKey:@"order_info"];
            self.orderDetailsModel = [[GYOrderDetails alloc] initWithDic:order_info];
             [self.orderRefundBtu setTitle:self.orderDetailsModel.if_lock == 1 ? @"退款中" : @"订单退款" forState:UIControlStateNormal];
            [self.tableView reloadData];
        }else if (code == 400) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"加载数据出错,请重新加载!"];
        }
    } error:^(NSError *error) {
        [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
        [EasyTextView showText:@"加载数据出错,请重新加载!"];
        NSLog(@"OrderDetailsFailed===%@",error);
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 4;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        GYOrderStateCell *cell = [GYOrderStateCell cellWithTableView:tableView];
        cell.orderTitleLable.text = @"交易状态";
        cell.orderStateLable.text = self.orderDetailsModel.state_desc;
    
        return cell;
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            GYReceiveTableViewCell *cell = [GYReceiveTableViewCell cellWithTbaleView:tableView];
            cell.receiveNameLable.text = self.orderDetailsModel.reciver_name;
            cell.receivePhoneLable.text = self.orderDetailsModel.reciver_phone;
            cell.receiveAddressLable.text = self.orderDetailsModel.reciver_addr;
            
            return cell;
        }else {
            GYOrderReceiveInfoCell *cell = [GYOrderReceiveInfoCell cellWithTableView:tableView];
            cell.receiveImageView.image = [UIImage imageNamed:self.images[indexPath.row-1]];
            cell.receiveTitlelable.text = self.titles[indexPath.row-1];
            switch (indexPath.row) {
                case 1:
                    cell.receiveContenLable.text = self.orderDetailsModel.order_message == nil ? @"" : self.orderDetailsModel.order_message;
                    break;
                    
                case 2:
                    cell.receiveContenLable.text = self.orderDetailsModel.invoice == nil ? @"" : self.orderDetailsModel.invoice;
                    break;
                    
                case 3:
                    cell.receiveContenLable.text = self.orderDetailsModel.payment_name == nil ? @"" : self.orderDetailsModel.payment_name;
                    break;
                    
                default:
                    break;
            }
            
            return cell;
        }
    }else if (indexPath.section == 2){
        GYOrderInfoTableViewCell *cell = [GYOrderInfoTableViewCell cellWithTableView:tableView];
        cell.customerServerCallBack = ^{
            GYWebViewViewController *WebCustomerServiceVC = [[GYWebViewViewController alloc] initWithNibName:@"GYWebViewViewController" bundle:nil];
            WebCustomerServiceVC.url = [NSURL URLWithString:@"https://youwei.yunhongkg.com/appapi/index.php?act=message&op=Customer"];
            [self.navigationController pushViewController:WebCustomerServiceVC animated:YES];
        };
        cell.orderDetails = self.orderDetailsModel;
        
        return cell;
    }else {
        GYOrdetTimeCell *cell = [GYOrdetTimeCell cellWithTableView:tableView];
        cell.model = self.orderDetailsModel;
        
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section != 3) {
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        bgView.backgroundColor = [UIColor clearColor];
        
        return bgView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return 0;
    }
    return 10;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            GYReceiveTableViewCell *cell = [GYReceiveTableViewCell cellWithTbaleView:tableView];
            return [cell getHeight:self.orderDetailsModel.reciver_addr];
        }else {
            return 60;
        }
    }else if (indexPath.section == 2){
        GYOrderInfoTableViewCell *cell = [GYOrderInfoTableViewCell cellWithTableView:tableView];
        
        return [cell getHeight:self.orderDetailsModel];
    }else {
        GYOrdetTimeCell *cell = [GYOrdetTimeCell cellWithTableView:tableView];
        
        return [cell getHeight:self.orderDetailsModel];
    }
}

#pragma mark -- 懒加载

- (NSArray *)titles {
    if (!_titles) {
        _titles = @[@"买家留言",@"发票信息",@"付款方式"];
    }
    
    return _titles;
}

- (NSArray *)images {
    if (!_images) {
        _images = @[@"mcc_04",@"mcc_08_b",@"mcc_06_b"];
    }
    
    return _images;
}

@end
